﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GsnHelper;
using ZhongheCms.Models;

namespace ZhongheCms.Controllers
{
    public class FaqController : Controller
    {
        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblContent.GetContentList(keywords, fid, GswTblUser.CurrentUser.CustomerId);
            return View(qry);
        }
        public ActionResult Create(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var cm = id == 0
                ? new GswTblContent { CreateDate = DateTime.Today, CustomerId = GswTblUser.CurrentUser.CustomerId, ContentNo = 1 } : GswTblContent.SingleOrDefault(id);
            if (id != 0) return View(cm);
            var qry =
                GswTblContent.FirstOrDefault("select max(ContentNo) as ContentNo from GswTblContent where CustomerId=" +
                                             GswTblUser.CurrentUser.CustomerId + " and FunctionId=" + fid);
            if (qry != null)
                cm.ContentNo = qry.ContentNo + 1;

            return View(cm);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection c, int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var cm = id == 0 ? new GswTblContent() : GswTblContent.SingleOrDefault(id);
            cm.ContentTitle = c["ContentTitle"];
            cm.CreateDate = DateTime.Today;
            cm.Author = "";
            cm.ContentBrief = c["ContentBrief"];
            cm.ContentDesc = c["ContentDesc"];
            cm.ContentPic = "";
            cm.ContentThumb = c["ContentThumb"];
            cm.FunctionId = int.Parse(fid);
            cm.CustomerId = GswTblUser.CurrentUser.CustomerId;
            cm.NewFlag = InputValid.StrToInt(c["NewFlag"], 0);
            cm.ContentNo = InputValid.StrToInt(c["ContentNo"], 0);
            try
            {
                if (ModelState.IsValid)
                {

                    if (id == 0)
                    {
                        cm.Insert();
                        cm = new GswTblContent { CreateDate = DateTime.Today, CustomerId = GswTblUser.CurrentUser.CustomerId, ContentNo = InputValid.StrToInt(c["ContentNo"], 0) + 1 };

                    }
                    else
                    {
                        cm.Update();
                    }
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";

            return View(cm);

        }

        public ActionResult Delete(int id, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblContent.Delete(id);
            var qry = GswTblContent.GetContentList(keywords, fid, GswTblUser.CurrentUser.CustomerId);
            return View("Index", qry);
        }

    }
}
